<?php if (!defined('BASEPATH'))
    exit ('No direct script access allowed');
    /**
     *    专辑
     * @author xiahuiqin
     *
     */
    class Column extends MY_Controller
    {
        public function  __construct()
        {
            parent::__construct();

            $this->load->model("column_mdl", 'column');
            $this->load->model("common_mdl", 'common');
            $this->load->model("member_mdl", 'member');
            $this->load->model("baby_mdl", 'baby');
            $this->load->model("focus_mdl", 'focus');
            $this->load->model("like_baby_mdl", 'like_baby');
            $this->load->model("baby_type_mdl", 'baby_type');
            
            $this->column_class = $this->common->get_list(array('sign' => 'column_class')) ? $this->common->get_list(array('sign' => 'column_class')) : array();

            $this->member_id    = $this->session->userdata('member_id');
            $this->member_name    = $this->session->userdata('member_name');
            $this->member_info = $this->member->get_one_list($this->member_id);
        }

        /**
         * 专辑首页
         */
        public function  index()
        {        	
        	$reders = array();
        	$memid = array();
        	$meminfo = array();
        	$focusinfo = array();
        	
        	$this->db->select('count(id) as all_c,memberid');
        	$this->db->group_by('memberid');
        	$this->db->order_by('all_c desc, updatetime desc');
        	$this->db->limit(5);
        	$reders = $this->db->get('column')->result_array();  //专辑红人(5个.  发表专辑最多的5个人.)
        	
        	if(count($reders) > 0){
        		foreach ($reders as $redk => $redv){
        			$memid[$redk] = $redv['memberid'];
        		
        			$meminfo[$redk] = $this->member->get_one_list($memid[$redk]);  //用户信息
        			$focusinfo[$redk] = count($this->focus->get_list(array('focuseder'=>$memid[$redk])));  //粉丝总数
        		}
        	}
        	
			$where         = '';
        	$limit         = '';
        	$offset        = '';
        	$order         = 'updatetime desc';
        	$data          = array();

        	$data['result']       = $this->column->get_list($where, $limit, $offset, $order);  //按更新时间排序,所有专辑
        	$data['column_info']  = $reders;  //专辑
        	$data['reder_info'] = $meminfo;  //专辑红人信息
        	$data['fans_num'] = $focusinfo;  //粉丝数
        	$data['cloth_class'] = $this->common->get_list('',10,'','id desc');  //宝贝分类
        	$this->load->view('column/index', $data);
        }

        /**
         * 个人首页
         */
        function center($id='7'){
        	$onemember = $this->member->get_one_list($id);
        	
            //$data['member'] = $onemember;
            $data['like_baby'] = $this->like_baby->get_list(array('liker_id'=>$onemember['id']),'','','addtime desc');  //喜欢的宝贝
            $data['focus'] = $this->focus->get_list(array('focuseder'=>$onemember['id']),'','','addtime desc');
            $data['mem_info'] = $onemember;
            $this->load->view('column/center', $data);
        }

        /**
         * 喜欢宝贝
         */
        function like_baby(){
            $baby_id = $this->input->post('baby_id');
            $like_num = count($this->like_baby->get_list(array('baby_id'=>$baby_id)));
            if(!empty($baby_id)){
                $one_lb = $this->like_baby->get_list(array('baby_id'=>$baby_id,'liker_id'=>$this->member_id));

                if(empty($one_lb)){  //如果这件宝贝没有被我喜欢过，则来喜欢
                    if($this->like_baby->insert(array('liker_id'=>$this->member_id,'baby_id'=>$baby_id,'addtime'=>date('Y-m-d H:i:s')))){
                        $like_num = count($this->like_baby->get_list(array('baby_id'=>$baby_id)));
                        $data = array('error'=>'<font color="red" style="font-size: 10px;">喜欢成功</font>','like_num' => $like_num);
                    }else{
                        $data = array('error'=>'<font color="red" style="font-size: 10px;">服务器有点累儿了～</font>','like_num' => $like_num);
                    }
                }else{
                    $data = array('error'=>'<font color="red" style="font-size: 10px;">小主您已喜欢过此宝咯～</font>','like_num' => $like_num);
                }
            }else{
                $data = array('error'=>'<font color="red" style="font-size: 10px;">未选中喜欢的宝贝</font>','like_num' => $like_num);
            }
            echo json_encode($data);
        }

        /**
         * 专辑列表
         */
        function column_list($member_id='7',$error = '')
        {
            $where         = array('memberid' => $member_id);
            $limit         = '';
            $offset        = '';
            $order         = 'id ASC';
            $data          = array();
            $data['error'] = $error;

            $data['column_class'] = $this->column_class ? $this->column_class : array();
            $data['result']       = $this->column->get_list($where, $limit, $offset, $order);
            $data['mem_info'] = $this->member->get_one_list($member_id);
            $this->load->view('column/column_listV', $data);
        }
        
        /**
         * 宝贝列表
         */
        function baby_list($member_id='7'){
        	$data['result'] = array();
        	
        	$where = array('liker_id'=>$member_id);
        	$order = 'addtime desc';
        	$data['result'] = $this->like_baby->get_list($where,'','',$order);
        	$data['mem_info'] = $this->member->get_one_list($member_id);
        	$this->load->view('column/baby_listV',$data);
        }


        /**
         * 关注列表
         */
        function focus_list($member_id='7'){
            $data = array();
            $focus = $this->focus->get_list(array('focuser'=>$member_id)) ? $this->focus->get_list(array('focuser'=>$member_id)) : '';  //关注列表
            $fans = $this->focus->get_list(array('focuseder'=>$member_id)) ? $this->focus->get_list(array('focuseder'=>$member_id)) : '';  //粉丝列表

            $data = array('focus'=>$focus,'fans'=>$fans);
            $data['mem_info'] = $this->member->get_one_list($member_id);
            $this->load->view('column/focusV',$data);
        }


        /**
         * 新增
         */
        function column_add()
        {
            $datas          = array();
            $datas['error'] = '';

            $url = front_url('column/column_list');

            if ($_POST) {
                $this->form_validation->set_rules('title', '专辑标题', 'trim|required|max_length[30]');
                $this->form_validation->set_rules('classid', '分类', 'trim|required');
                $this->form_validation->set_rules('desc', '专辑描述', 'trim|max_length[120]');

                if ($this->form_validation->run() == FALSE) {
                    $datas['column_class'] = $this->column_class;

                    redirect($url);
                } else {
                    $datas               = $this->_post_data();
                    $datas['addtime']    = date('Y-m-d H:i:s');
                    $datas['updatetime'] = date('Y-m-d H:i:s');
                    $datas['img'] = $this->_upload_img('img','local_first_add');
                    //var_dump($datas['img']);exit;
                    if ($id = $this->column->insert_id($datas)) {
                        redirect('column/detail/' . $id);
                    } else {
                        $error = "创建专辑失败！";
                        redirect('column/column_list/' . $error);
                    }
                }
            }
            redirect($url);
        }

        /**
         * 专辑详情页
         * @param 专辑id $id
         */
        function detail($id = '')
        {
        	$one_column = $this->column->get_one_list($id);
            $data['result'] = $one_column;

            $data['mem_info'] = $this->member->get_one_list($one_column['memberid']); //上传该专辑的用户相关信息
            
            $allcreatecolumn_sql = $this->column->get_list(array('memberid'=>$one_column['memberid']));
            $data['column_nums'] = count($allcreatecolumn_sql);  //专辑数
            
            $data['baby_nums'] = count($this->like_baby->get_list(array('column_id'=>$id)));  //宝贝数

            $this->load->view('column/detailV', $data);
        }
        
        /**
         * 将宝贝加入专辑
         */
        function baby_like_add(){
        	$baby_url = $this->input->post('baby_url');
        	$bid = strstr($baby_url,'baby_detail/');
        	$bid = substr($bid,12);
        	        	
        	if($this->input->post('column_id')){
        		$tmp['column_id'] = $this->input->post('column_id');
        		$tmp['desc'] = $this->filer_chars($this->input->post('desc'));
        		$tmp['baby_id'] = $bid;
        		$tmp['addtime'] = date("Y-m-d H:i:s");
        		$tmp['liker_id'] = $this->member_id;

                //看看之前有没有将这个宝贝加入这个专辑
                $isadd_before = $this->like_baby->get_list(array('baby_id'=>$bid,'column_id'=>$this->input->post('column_id')));

                if(empty($isadd_before)){
                    $this->like_baby->insert($tmp);
                }
                //这里是少了一个判断的,如果是有加过，应该有一个提示语
                $url = front_url('column/detail').'/'.$this->input->post('column_id');

                redirect($url);
        	}else{
        		redirect('column/index');
        	}
        }

        /**
         * 编辑
         */
        function column_edit()
        {
            $id = $this->uri->segment('3');

            if ($this->input->post()) {
                $datas               = $this->_post_data();
                $datas['updatetime'] = date('Y-m-d H:i:s');
                $datas['img']        = $this->_upload_img('img', '1');

                if ($this->column->update_byfield('id', $id, $datas)) {
                    //$error = "更新专辑信息成功！";
                    redirect('column/detail/' . $id);
                } else {
                    $data['error'] = '更新失败！';
                }
            } else {
                $data['error'] = '';
            }

            $data['one'] = $this->column->get_one_list($id);
            $data['column_class'] = $this->column_class;
            
            $this->load->view('column/column_editV', $data);
        }

        /**
         * 删除
         */
        function column_delete()
        {
            $id  = $this->uri->segment(3);
            $con = array('id' => $id,'memberid' => $this->member_id);

            $this->column->del($con);

            $this->column_list();
        }

        /*
         * post数据（add + edit）
         */
        function _post_data()
        {
            $datas['title']    = $this->input->post('title'); //专辑标题
            $datas['classid']  = $this->input->post('classid'); //专辑分类
            $datas['desc']     = $this->input->post('desc'); //描述
            $datas['memberid'] = $this->session->userdata('member_id');

            return $datas;
        }


        /**
         * 专辑图片上传
         * @param unknown_type $img_name
         * @param unknown_type $img_url
         * @return Ambigous <string, unknown>
         */
        function _upload_img($img_name, $img_url)
        {
            $config ['upload_path']   = './upload/column/'; //设置图片上传目录
            $config ['allowed_types'] = 'gif|jpg|png';
            $config ['max_size']      = 210 * 210;
            $config ['encrypt_name']  = TRUE;

            $this->load->library('upload', $config);
            if ($_FILES[$img_name]['size'] && $this->upload->do_upload($img_name)) {
                if ($img_url != 'local_first_add') { //如果不是第一次增加图片，即是更改图片
                    $url = './upload/column/' . $img_url;
                    unlink($url); //删除原图
                }
                //取得上传成功数据
                $data = $this->upload->data();
                $img  = $data['file_name'];
            } else {
                if ($this->input->post($img_name)) {
                    $img = $this->input->post($img_name); //图片
                } else {return '1';
                    $img = '';
                }
            }
            return $img;
        }
    }